import Geocoder from "./Geocoder.js";
import jwx from "@/common/jwx.js";
export default function(mpt, callback) {
	let promise;
	if (typeof callback !== "function") {
		promise = new Promise((resolve, reject) => {
			callback = function(resolvedata, rejectdata) {
				if (resolvedata) {
					let convertor = new BMap.Convertor();
					var ggPoint = new BMap.Point(resolvedata.lng, resolvedata.lat);
					var pointArr = [];
					pointArr.push(ggPoint);
					convertor.translate(pointArr, 3, 5, async function(data) {
						// let wz = await Geocoder(data.points[0])
						resolve(data.points[0]);
					});
					// resolve(resolvedata);
				} else {
					reject(rejectdata);
				}
			};
		});
	}
	mpt = mpt || "";
	mpt = mpt.match(/\d+\.\d+/g);

	function bd09togcj02(bd_lon, bd_lat) {
		var x_pi = (3.14159265358979324 * 3000.0) / 180.0;
		var x = bd_lon - 0.0065;
		var y = bd_lat - 0.006;
		var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
		var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
		var gg_lng = z * Math.cos(theta);
		var gg_lat = z * Math.sin(theta);
		return [gg_lng, gg_lat];
	}
	var data = bd09togcj02(mpt[0], mpt[1]);
	if (uni.$z.IsWeixinOrAlipay() == "weixin") {
		jwx.openlocation({
			latitude: parseFloat(data[1]), // 纬度，浮点数，范围为90 ~ -90
			longitude: parseFloat(data[0]), // 经度，浮点数，范围为180 ~ -180。
		});
	} else {
		uni.openLocation({
			latitude: Number(data[1]),
			longitude: Number(data[0]),
			success: function() {
				console.log('success');
			}
		});
	}

	// uni.getLocation({
	// 	type: 'gcj02',
	// 	success: function(res) {
	// 		console.log('当前位置的经度：' + res.longitude);
	// 		console.log('当前位置的纬度：' + res.latitude);
	// 	}
	// });


	// Geocoder({
	// 	lat: 30.346398997089644,
	// 	lng: 120.12302981536222
	// })


	// uni.getLocation({
	// 	type: "wgs84",
	// 	success: function(res) {
	// 		callback({
	// 			lng: res.longitude,
	// 			lat: res.latitude,
	// 		});
	// 		console.log("当前位置的经度：" + res.longitude);
	// 		console.log("当前位置的纬度：" + res.latitude);
	// 	},
	// });
	return promise;
}